﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SQLite;
using System.Threading;
using System.Net;
using System.IO;


namespace post
{
    public partial class Main : Office2007Form
    {
        public Main()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        dll.dbhelp db = new dll.dbhelp();//数据库操作类
        dll.Https https = new dll.Https();//http操作类
        private void 新建任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            task.add a = new task.add();
            a.ShowDialog();
        }
        /// <summary>
        /// 加载任务
        /// </summary>
        public void loadtask()
        {

        }

        private void Main_Load(object sender, EventArgs e)
        {
            load_task();
        }

        public void load_task()
        {
            string sql = "select * from ch_task";
            SQLiteCommand comm = new SQLiteCommand(sql);
            DataTable dt = db.select(comm);
            foreach (DataRow item in dt.Rows)
            {
                TreeNode node = new TreeNode();
                node.Text = item["title"].ToString();
                node.Tag = item["id"].ToString();
                treeView1.Nodes.Add(node);
            }
           
        }

        private void 执行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode!=null)
            {
                Thread th = new Thread(new ParameterizedThreadStart(st));
                th.Start(treeView1.SelectedNode.Tag); 
            }
        }

        private void st(object obj)
        {
            //返回任务
            int id = int.Parse(obj.ToString());
            model.Task task = new model.Task();
            string sql = "select * from ch_task where id=@id";
            SQLiteCommand comm = new SQLiteCommand(sql);
            SQLiteParameter[] par = new SQLiteParameter[] {
                new SQLiteParameter("@id",id)
            };
            comm.Parameters.AddRange(par);
            DataTable dt = db.select(comm);
            task.id = int.Parse(dt.Rows[0]["id"].ToString());
            task.title = dt.Rows[0]["title"].ToString();
            task.interval = int.Parse(dt.Rows[0]["interval"].ToString());
            task.noqj = int.Parse(dt.Rows[0]["noqj"].ToString());
            task.timerqj = int.Parse(dt.Rows[0]["timerqj"].ToString());
            task.patid = dt.Rows[0]["patid"].ToString();
            task.types = dt.Rows[0]["types"].ToString();
            task.oks = dt.Rows[0]["oks"].ToString();
            task.nos = dt.Rows[0]["nos"].ToString();
            //返回任务的参数
            List<model.Parameter> listpar = new List<model.Parameter>();
            sql = "select * from ch_parameter where tid=@tid";
            comm = new SQLiteCommand(sql);
            par = new SQLiteParameter[] {
                new SQLiteParameter("@tid",task.id)
            };
            comm.Parameters.AddRange(par);
            dt = db.select(comm);
            foreach (DataRow item in dt.Rows)
            {
                model.Parameter p = new model.Parameter();
                p.id = int.Parse(item["id"].ToString());
                p.name = item["name"].ToString();
                p.tid = int.Parse(item["tid"].ToString());
                sql = "select * from ch_zf where parid=@parid";
                comm = new SQLiteCommand(sql);
                par = new SQLiteParameter[] {
                    new SQLiteParameter("@parid",int.Parse(item["id"].ToString()))
                };
                DataTable dts = db.select(comm);
                foreach (DataRow items in dts.Rows)
                {
                    model.Zf zf = new model.Zf();
                    zf.id = int.Parse(items["id"].ToString());
                    zf.lasid = int.Parse(items["lasid"].ToString());
                    zf.parid = int.Parse(items["parid"].ToString());
                    zf.str = items["str"].ToString();
                    zf.tid = int.Parse(items["tid"].ToString());
                    p.zfs.Add(zf);
                }
                listpar.Add(p);
            }
            switch (task.patid)
            {
                case"爆破":
                    bp(listpar,task);
                    break;
                case"普通":
                    pt(listpar);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 爆破的方法
        /// </summary>
        /// <param name="t"></param>
        public void bp(List<model.Parameter> pars,model.Task t)
        {
            for (int i = 0; i < pars[0].zfs.Count; i++)
            {
                foreach (model.Parameter item in pars)
                {
                    for (int j = 1; j < item.zfs.Count; j++)
                    {
                        Encoding encoding = Encoding.GetEncoding("utf-8");
                        IDictionary<string, string> parameters = new Dictionary<string, string>();
                        parameters.Add("UserName", UserName);
                        parameters.Add("Password", Password);
                        HttpWebResponse response = https.CreatePostHttpResponse("adfasdf.com", parameters, encoding);
                        Stream stream = response.GetResponseStream(); //获取响应的字符串流 
                        StreamReader sr = new StreamReader(stream); //创建一个stream读取流 
                        string html = sr.ReadToEnd(); //从头读到尾，放到字符串html
                        return;
                    }
                }
            }
        }
        /// <summary>
        /// 普通的方法
        /// </summary>
        /// <param name="k"></param>
        public void pt(List<model.Parameter> pars)
        {

        }

    }
}
